//
//  FillOutViewController.m
//  Cheirapsis
//
//  Created by 田宇 on 15/11/3.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "FillOutViewController.h"
#import "ServeProjectViewController.h"
#import "Define.h"
#import "iToast.h"

@interface FillOutViewController ()

@end

@implementation FillOutViewController{
    UITextField *nameText;
    UITextField *telephoneText;
    UITextField *remarksText;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"填写订单";
    _dateSource = @[
                  @{
                    @"headerTitle":@"订单信息",
                    @"list":@[
                          @{@"cellTitle":@"店名"},
                          @{@"cellTitle":@"项目"},
                          ]
                    },
                  @{
      @"headerTitle":@"联系人信息",
      @"list":@[
              @{@"cellTitle":@"联系人"},
              @{@"cellTitle":@"联系电话"},
              @{@"cellTitle":@"备注"}
              ]
      }
  ];
    nameText.returnKeyType = UIReturnKeyDone;
    telephoneText.returnKeyType = UIReturnKeyDone;
    remarksText.returnKeyType = UIReturnKeyDone;
    self.tableView.scrollEnabled = NO;
}
- (IBAction)disapp:(id)sender {
    [nameText resignFirstResponder];
    [telephoneText resignFirstResponder];
    [remarksText resignFirstResponder];
}

// cell的绘制
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableviewcell"];
    NSDictionary *dic = [_dateSource objectAtIndex:indexPath.section];
    NSArray *dicA = dic[@"list"];
    NSDictionary *smalldic = [dicA objectAtIndex:indexPath.row];
    
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UILabel *headingLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 10*F_HEIGHT, 80, 25*F_HEIGHT)];
    headingLab.text =smalldic[@"cellTitle"];
    UILabel *hengLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 45*F_HEIGHT, WIDTH-15, 1*F_HEIGHT)];
    hengLab.backgroundColor = [UIColor lightGrayColor];
    [cell addSubview:hengLab];
    [cell addSubview:headingLab];
    
    
    UILabel *dianMingLab = [[UILabel alloc]initWithFrame:CGRectMake(100, 10*F_HEIGHT, WIDTH-115, 25*F_HEIGHT)];
    dianMingLab.textAlignment =2;
    [cell addSubview:dianMingLab];
    if (indexPath.section==0&&indexPath.row==0) {
        dianMingLab.text = _dic[@"storeName"];
    }else if(indexPath.section==0&&indexPath.row==1){
    dianMingLab.text = _dic[@"storeServiceType"][_k];
    }
    
    if (indexPath.section==1&&indexPath.row==0) {
    nameText = [[UITextField alloc]initWithFrame:CGRectMake(100, 10*F_HEIGHT, WIDTH-115, 25*F_HEIGHT)];
        nameText.placeholder = @"请填写姓名";
        nameText.textAlignment = 2;
        [cell addSubview:nameText];
    }
    
    if (indexPath.section==1&&indexPath.row==1) {
    telephoneText = [[UITextField alloc]initWithFrame:CGRectMake(100, 10*F_HEIGHT, WIDTH-115, 25*F_HEIGHT)];
        telephoneText.placeholder = @"请输入电话号";
        telephoneText.textAlignment = 2;
        [cell addSubview:telephoneText];
    }
    
    if (indexPath.section==1&&indexPath.row==2) {
        remarksText = [[UITextField alloc]initWithFrame:CGRectMake(100, 10*F_HEIGHT, WIDTH-115, 25*F_HEIGHT)];
        remarksText.placeholder = @"选填";
        remarksText.textAlignment = 2;
        [cell addSubview:remarksText];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45*F_HEIGHT;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = [_dateSource objectAtIndex:section];
    NSArray *dicA = dic[@"list"];
    return dicA.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dateSource.count;
}
// header 的编写
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    NSDictionary *dic = [_dateSource objectAtIndex:section];
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40*F_HEIGHT)];
    headerView.backgroundColor = BACKGROUNDGRAY;
    
    UILabel *headerLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 13*F_HEIGHT, 90, 24*F_HEIGHT)];
    headerLab.text = dic[@"headerTitle"];
    [headerView addSubview:headerLab];
    
    [self.view addSubview:headerView];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40*F_HEIGHT;
}

//  footer 的编写
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        
    
    UIView *footerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50*F_HEIGHT)];
    footerview.backgroundColor = ALL233COROL;
    [self.view addSubview:footerview];
    
    UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 5*F_HEIGHT, WIDTH-30, 40*F_HEIGHT)];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    nextBtn.backgroundColor = [UIColor colorWithRed:159/255.0f green:203/255.0f blue:239/255.0f alpha:1];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nextBtn.layer.cornerRadius = 5.0f;
    nextBtn.layer.masksToBounds = YES;
    [nextBtn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    
    [footerview addSubview:nextBtn];
    
    return footerview;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==1) {
        return 50*F_HEIGHT;
    }
    return 0.1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 下一步跳转
-(void)next{
    if (!nameText.text.length) {
        [[iToast makeText:@"请填写联系人"] show];
        return;
    }else if (!telephoneText.text.length) {
        [[iToast makeText:@"请填写练习电话"] show];
        return;
    }
    ServeProjectViewController *service = [[ServeProjectViewController alloc]init];
    service.dic = _dic;
    service.k = _k;
    service.nameString = nameText.text;
    service.phoneNumberString = telephoneText.text;
    service.remarksString = remarksText.text;
    
    service.dateString = _dateString;
    [self.navigationController pushViewController:service animated:YES];
}


@end
